package com.market.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.transients.Email;

@Controller("emailController")
@Scope("session")
public class EmailController {

	public void sendEmail(Email email) {
		Properties properties = System.getProperties();
		properties.put("mail.smtp.host", "localhost");
		properties.put("mail.smtp.port", "25");
		properties.put("mail.smtp.username", email.getFrom());
		properties.put("mail.smtp.password", "Tony4508");
		Session session = Session.getDefaultInstance(properties);
		MimeMessage message = new MimeMessage(session);
		
		try {
			message.setHeader("Content-Type", "text/plain; charset=UTF-8");
			message.setFrom(new InternetAddress(email.getFrom(), email.getPersonalName()));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					email.getTo()));
			message.setSubject(email.getSubject(), "utf-8");
//			message.setText(email.getText(),"utf-8","html");	
			message.setText(email.getContent(), "utf-8","html");
			message.setSentDate(new java.util.Date());
			Transport.send(message);
			System.out.println("Send email done to " + email.getTo() + ":" + email.getSubject());
		} catch (AddressException e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed",
							"Send email"));
			e.printStackTrace();
		} catch (MessagingException e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed",
							"Send email"));
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed",
							"Send email"));
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*
	 * public void sendEmail(String to, String subject, String text) {
	 * this.init(); Session session = Session.getDefaultInstance(properties);
	 * 
	 * System.out.println("From:" + from); System.out.println("To:" + to);
	 * System.out.println("webSiteName:" + webSiteName);
	 * System.out.println("mail.smtp.host:" +
	 * properties.getProperty("mail.smtp.host"));
	 * System.out.println("mail.smtp.port:" +
	 * properties.getProperty("mail.smtp.port"));
	 * System.out.println("mail.smtp.username:" +
	 * properties.getProperty("mail.smtp.username"));
	 * System.out.println("mail.smtp.password:" +
	 * properties.getProperty("mail.smtp.password"));
	 * 
	 * MimeMessage message = new MimeMessage(session); try { message.setFrom(new
	 * InternetAddress(from, webSiteName));
	 * message.addRecipient(Message.RecipientType.TO, new InternetAddress( to));
	 * message.setSubject(subject); message.setText(text);
	 * message.setSentDate(new java.util.Date()); Transport.send(message); }
	 * catch (AddressException e) { e.printStackTrace(); } catch
	 * (MessagingException e) { e.printStackTrace(); } catch
	 * (UnsupportedEncodingException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); } }
	 */

}
